var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;


    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });
    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["code"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }

    let salesmans=[];
    let supplierTypes=[];
    let paymentTypes=[];
    let deptSalesmanList=[];
    let levels=[];
    layer.ready(function () {
        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if(treeList && treeList.list){
            let userTree = getAllJson(treeList.list);
            salesmans=getxmselect("#salesmanCode","salesmanCode", userTree);
        }else {
            salesmans=getxmselect("#salesmanCode","salesmanCode", [{name: user.name, value: user.code}]);
        }

        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", deptTree);
        } else {
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", [{name: user.name, value: user.code}]);
        }

        //获取供应商信息
        $.ajax({
            url: "payment/getSupplierInfo",
            success: function (data) {
                if (data.status == 200) {
                    for (let i in data.data) {
                        $('#supplierCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });
        let typeList=getdictionary(6,36);//客户类型
        if(typeList.length>=0 ){
            let typeTree = getAllJson(typeList);
            supplierTypes=getxmselect("#supplierType","supplierType", typeTree);
        }else {
            supplierTypes=getxmselect("#supplierType","supplierType", []);
        }

        let paymentTypeList = getdictionary(5, 17);//付款类型
        if(paymentTypeList.length>=0){
            let typeTree = getAllJson(paymentTypeList);
            paymentTypes=getxmselect("#paymentType","paymentType", typeTree);
        }else {
            paymentTypes=getxmselect("#paymentType","paymentType", []);
        }

        let levelList=getdictionary(6,35);//供应商等级
        if(levelList.length>=0){
            let typeTree = getAllJson(levelList);
            levels=getxmselect("#levels","level", typeTree);
        }else {
            levels=getxmselect("#levels","level", []);
        }

    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#collectionSelect'
        ,data:initData
        ,toolbar: '#collectionToolbar'
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号'}
            , {field: 'supplierName', title: '供应商名称', sort: true,width: 250}
            , {field: 'supplierNo', title: '供应商代码', sort: true,width: 150}
            , {field: 'supplierCode', title: '供应商编码', sort: true,width: 150}
            , {field: 'supplierType', title: '供应商类型', sort: true,width: 150}
            , {field: 'levelName', title: '供应商等级', sort: true,width: 150}
            , {field: 'salesmanName', title: '采购员',width: 150}
            , {field: 'address', title:'供应商地址',width: 250}
            , {field: 'payday', title: '账期', sort: true,width: 150}
            , {field: 'paymentType', title:'付款类型',sort:true,width: 150}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#collectionSelect').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'customer2/supAccDeCollListByPage',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(collectionExcel)', function (data) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'customer2/supAccDeCollExport',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '供应商账期详情.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });


    $("#clear").on("click", function() {
        salesmans.setValue([ ]);
        supplierTypes.setValue([ ]);
        paymentTypes.setValue([ ]);
        deptSalesmanList.setValue([ ]);
        levels.setValue([ ]);
    });
});
